Cuestionario Ampliado del Censo de Población y Vivienda 2000

El cuestionario ampliado se guarda en un un archivo .RData.

data <- read_sav("~/Cuestionario Ampliado_2000_Persona.sav")

save(data, 
      file = paste0(here::here(),"/Bases/Censo_Personas_2000.RData"))

Se seleccionan las variables que se desean conservar para la realización de este documento y se guarda en un archivo .RData para practicidad del manejo de datos.

Posibles variables que se pueden contemplar en la migración reciente

  • EDAD
  • SEXO
  • AFRODES 
  • HLENGUA 
  • QDIALECT_C
  • PERETN 
  • NIVACAD
  • ALFABET
  • ESTCON 
  • CONACT 
  • HIJOS_NAC_VIVOS

La variable mydata contiene 10 099 182 observaciones y 12 variables.

load(paste0(here::here(),"/Bases/Censo_Personas_2000.RData"))

mydata <- data %>%
           select(CVE_ENT, NOM_ENT, MUN, CVE_MUN, NOM_MUN, CVE_MUN_RES, RES95EDO_C, MUN95OTR_C, CAUEMI,
                  EDAD, SEXO, HLENGUA, QDIALECT_C, PERETN, ESTCON, NIVACAD, ESCOLARI, ALFABET, 
                  CONACT, SITTRA, FACTOR, ESTRATO, UPM) %>%
             mutate(CVE_ENT = str_pad(.$CVE_ENT, width = 3, side = c("left"), pad = "0"))

save(mydata, file = paste0(here::here(), "/Bases/01_Migracion interna_2000.RData"))

✔️A partir de aquí se pueden correr los códidos 👇.

Se carga el archivo Migracion interna_2000.RData.

load(paste0(here::here(), "/Bases/01_Migracion interna_2000.RData"))

# Para fines prácticos se genera un ponderador de uno 
mydata <- mydata %>%
           select(CVE_ENT, NOM_ENT, CVE_MUN, NOM_MUN, RES95EDO_C, MUN95OTR_C, CVE_MUN_RES, EDAD, FACTOR, ESTRATO, UPM) %>%
            mutate(M = 1)  %>%
             mutate(NOM_ENT = as.factor(.$CVE_ENT)) %>%
              ungroup()

Claves de entidades y municipios

Se genera un vector con el nombre de las entdades llamado estados para facilitar los filtros en el documento. 
Se genera un vector con las abreviaturas de las entidades llamado est para fines prácticos. 
Se genera un vector con las claves de los municipios.

# Claves de los estados
estados <- sjlabelled::get_labels(mydata$CVE_ENT)

nom_estados <- c( "Aguascalientes", "Baja California" ,"Baja California Sur", "Campeche", "Coahuila de Zaragoza",
                  "Colima", "Chiapas", "Chihuahua", "Ciudad de México", "Durango", 
                  "Guanajuato", "Guerrero", "Hidalgo", "Jalisco",  "México", 
                  "Michoacán de Ocampo", "Morelos", "Nayarit", "Nuevo León", "Oaxaca", 
                  "Puebla", "Querétaro", "Quintana Roo", "San Luis Potosí", "Sinaloa", 
                  "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala","Veracruz de Ignacio de la Llave", 
                  "Yucatán", "Zacatecas")

est <- c("AGS", "BC", "BCS", "CAMP", "COAH", "COL", "CHIS", "CHIH", "CDMX", "DGO", "GTO", "GRO", "HGO",
         "JAL", "MEX", "MICH", "MOR", "NAY", "NL", "OAX", "PUE", "QRO", "QROO", "SLP","SIN","SON", "TAB", 
         "TAMS", "TLX", "VER", "YUC", "ZAC")

# Claves de los municipios
MUN <- readRDS(paste0(here::here(), "/Bases/municipios_2000.RDS"))
nom_municipios <- sjlabelled::get_labels(MUN$NOM_MUN) %>% as.factor()
municipios <- sjlabelled::get_labels(MUN$CVE_MUN) %>% as.factor()

# Se le asignan las etiquetas a los nombres de los estados 
levels(mydata$NOM_ENT) <- estados

Población de 5 años y más

Se identifica a la población de 5 años y más.

filter(EDAD >= 5 & EDAD != 999)'.

Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <= 130) %>%
                 filter(RES95EDO_C %in% estados)  # Filtro del lugar de residencia dentro del país. 

Nivel estatal

Migración reciente

Se utiliza la paquetería survey para poder trabajar con la muestra del cuestionario ampliado, en la cual se selecciona a la población de 5 años y más.

options(survey.lonely.psu = "adjust")

MC <- mydata %>%
       as.data.frame() %>%
        mutate(EDAD = as.numeric(.$EDAD)) %>%
         subset(EDAD >= 5 & EDAD <= 130) %>%
          filter(RES95EDO_C %in% estados) %>%
           svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2000/MC_estado.RDS"))

Se genera una matriz cruzada del lugar de residencia hace 5 años a nivel estatal, utilizando la función svytable de la paquetería survey.

MC <- readRDS(paste0(here::here(), "/Output/Estado/01_Migracion reciente/2000/MC_estado.RDS")) 

Migrantes <- svytable(~RES95EDO_C + CVE_ENT, design = MC) 

La función cross_cases() de la paquetería expss se utiliza para crear tablas de contingencia cruzadas a partir de dos o más variables categóricas. Utilizando el comando weight, permite ponderar las observaciones “factores de expansión” en la tabla.

Se quita la diagonal a la matriz cruadrada con la función diag.remove() de la paquetería sna, donde esta función reemplaza los elementos de la diagonal principal de una matriz por un valor nulo o por el valor especifico.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_ENT, RES95EDO_C, weight = Freq) %>%
                as.data.frame() %>%
                 slice(-33) %>% 
                  select(-row_labels) 

rownames(Migrantes)<- nom_estados
colnames(Migrantes) <- nom_estados

save(Migrantes, file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2000/Matriz de migracion reciente a nivel estatal 1995-2000.RData"))

wb <- createWorkbook()
addWorksheet(wb, "MREst. 1995-2000")
writeData(wb, 1, Migrantes, colNames = TRUE, rowNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2000/Matriz de migracion reciente a nivel estatal 1995-2000.xlsx"), overwrite = TRUE)

Matriz de migración reciente hace 5 años a nivel estatal, 1995-2000

Matriz de migración reciente 1995-2000
Nivel estatal
Entidad Aguascalientes Baja California Baja California Sur Campeche Coahuila de Zaragoza Colima Chiapas Chihuahua Ciudad de México Durango Guanajuato Guerrero Hidalgo Jalisco México Michoacán de Ocampo Morelos Nayarit Nuevo León Oaxaca Puebla Querétaro Quintana Roo San Luis Potosí Sinaloa Sonora Tabasco Tamaulipas Tlaxcala Veracruz de Ignacio de la Llave Yucatán Zacatecas
Aguascalientes 772422 592 167 0 603 201 144 993 7878 589 2323 404 452 7931 5.5e+03 617 353 340 904 414 702 558 92 1624 229 308 304 760 160 726 49 6356
Baja California 1211 1885490 3773 156 1202 3168 10581 2812 13443 5016 6623 10780 3885 21913 1.1e+04 15222 2272 12717 1263 11899 6430 1104 476 846 50806 26297 1340 857 591 20479 264 1850
Baja California Sur 43 3434 332660 78 355 300 360 337 2304 532 367 7747 307 2546 2.1e+03 782 274 587 260 1195 987 100 173 206 7544 1929 84 117 31 1233 181 79
Campeche 0 169 6 574209 162 27 3215 119 2111 62 73 199 143 362 8.8e+02 207 228 31 131 427 573 43 3259 162 266 24 8700 744 108 7666 3518 11
Coahuila de Zaragoza 1048 1020 276 207 1945192 156 861 7360 3556 9632 1300 684 940 1921 3.2e+03 1176 472 283 12221 1080 1312 680 193 2027 643 457 810 4413 58 9623 148 5583
Colima 106 943 159 40 157 441228 446 199 2170 190 584 2590 269 11866 1.2e+03 5732 273 564 401 463 262 130 59 148 586 334 307 422 22 575 202 275
Chiapas 32 794 184 1079 100 107 3324721 324 6765 76 659 708 258 1226 4.7e+03 535 563 531 634 5746 2306 196 1104 79 382 144 8336 741 294 7408 420 124
Chihuahua 888 1889 310 262 17972 341 3088 2500018 6677 34501 1834 1991 1773 3583 5.0e+03 1519 613 274 2931 4600 2708 993 533 1215 6336 4222 1525 948 310 36612 121 9394
Ciudad de México 1380 3666 817 898 1645 551 7977 2976 7335085 805 6565 12191 17309 9969 1.9e+05 12755 9480 671 4174 27180 32722 5246 2499 3194 2727 2268 2604 2587 5001 32408 1513 1007
Durango 649 2261 342 0 13094 51 157 7239 1688 1214838 480 290 491 1219 1.8e+03 485 150 277 1197 89 241 169 256 312 1977 424 38 479 0 717 86 2115
Guanajuato 1667 2394 650 79 1575 454 419 1110 21405 909 3926705 1658 1584 9753 2.0e+04 8527 1409 680 1937 1028 1303 6193 220 2932 1080 1025 301 1652 519 3285 161 1100
Guerrero 46 771 655 116 240 663 1043 196 11539 122 497 2579674 520 2057 9.9e+03 5627 7048 476 202 3714 1819 488 557 271 932 578 335 607 327 2384 68 176
Hidalgo 334 1046 161 35 500 179 553 733 28447 291 1593 988 1864655 1549 3.7e+04 1183 675 75 1315 864 5791 2029 186 2086 247 209 421 984 1411 6792 58 279
Jalisco 4062 8237 2179 218 1320 9581 2848 2289 18592 1592 7485 6558 2783 5341337 1.2e+04 19108 1060 12263 2716 3040 3645 2129 632 3967 8986 3257 1340 2065 586 7724 376 6248
México 1398 3129 490 607 2146 1030 10611 2625 501099 1189 10376 17107 27770 10472 1.1e+07 17988 9995 448 2883 28508 39417 6272 2623 4361 2893 1197 2241 2618 6513 45383 1392 1383
Michoacán de Ocampo 254 3224 486 467 529 2091 926 610 19419 589 6997 12202 535 11301 1.5e+04 3346484 2139 1070 808 2267 1213 1174 114 1235 1606 1317 240 1329 198 2165 231 214
Morelos 127 684 212 43 317 192 944 245 26655 134 888 19262 1003 1841 1.8e+04 1537 1273480 166 545 2017 5000 655 529 364 77 266 716 432 473 3925 29 132
Nayarit 222 4213 749 84 376 431 500 344 1538 501 526 949 181 12990 1.5e+03 2249 88 764879 203 436 209 69 105 138 2943 1052 287 119 17 481 29 425
Nuevo León 803 1346 389 280 14419 103 2080 3080 9141 2424 2968 1379 2991 4140 6.2e+03 1851 477 328 3250584 2063 1901 1109 670 22256 1879 1696 1802 23728 351 18780 472 4069
Oaxaca 151 2602 887 273 429 226 5661 255 17443 254 318 3715 545 1354 1.5e+04 897 1671 77 352 2906358 5246 298 699 255 1152 509 918 836 372 18194 393 172
Puebla 329 1566 157 563 585 335 3570 977 30794 92 1374 3095 3748 1901 2.5e+04 1357 3915 155 1132 9620 4277152 1244 1182 423 691 697 2478 1112 7581 29430 337 384
Querétaro 481 685 154 53 574 290 787 351 24745 269 9103 607 3800 1646 1.9e+04 3817 973 500 1041 873 1413 1142717 314 2275 409 339 496 740 376 3540 205 291
Quintana Roo 287 151 155 6001 310 180 12018 471 12689 66 961 4372 687 1768 6.8e+03 918 1112 64 1193 2015 3419 452 633159 304 508 264 17144 476 211 18941 29556 79
San Luis Potosí 974 349 9 29 1881 45 144 606 7000 256 3518 412 1790 2587 6.0e+03 1134 385 127 9135 528 798 1504 233 1940412 199 477 311 8218 237 3437 145 3017
Sinaloa 145 10144 2150 170 480 374 572 2826 3401 5535 826 22133 244 5459 1.8e+03 642 353 3611 861 9979 814 282 183 439 2139710 9882 224 476 116 2211 32 324
Sonora 284 9272 1220 91 540 693 1464 5934 2886 1502 1650 1809 463 4260 1.2e+03 3171 412 2921 801 2653 1165 218 136 278 27191 1852115 403 590 33 3482 133 497
Tabasco 8 220 93 4299 255 14 11855 260 3203 52 255 171 276 837 2.3e+03 647 447 68 468 1665 1342 32 1815 163 382 30 1611285 654 151 11132 1299 109
Tamaulipas 471 1042 162 840 5886 496 3499 1160 7977 1548 2220 2202 2819 2893 5.7e+03 1842 588 557 16857 2470 2523 795 456 19949 939 261 3510 2240017 157 78179 360 1207
Tlaxcala 187 500 199 86 253 43 447 258 11330 39 456 343 1377 551 7.8e+03 421 301 57 197 537 13026 196 60 196 32 88 82 224 798486 3578 118 19
Veracruz de Ignacio de la Llave 620 2019 354 2912 959 551 6273 1356 32753 512 1950 2910 3783 3313 2.5e+04 2048 2454 499 2866 16845 21652 889 5202 2070 1904 1350 10590 14227 1855 5927396 1360 472
Yucatán 76 192 60 6583 21 0 1784 169 5767 93 372 227 88 456 2.8e+03 222 228 51 471 480 806 376 12881 61 386 100 4525 156 53 4058 1426618 411
Zacatecas 2763 994 59 185 2160 98 139 2861 2644 3445 1405 229 155 7676 2.9e+03 300 98 654 1523 127 243 247 127 1768 458 376 89 728 220 659 47 1131817
Fuente: Estimaciones del CONAPO.

Gráfico dinámico

Gráfico dinámico de migración reciente a nivel estatal.

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2000/Matriz de migracion reciente a nivel estatal 1995-2000.RData"))

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0) 

names <- c("Aguascalientes", "Baja California" ,"Baja California Sur", "Campeche", "Coahuila", "Colima", 
           "Chiapas", "Chihuahua", "Ciudad de México", "Durango", "Guanajuato", "Guerrero", "Hidalgo", "Jalisco",    
           "México", "Michoacán", "Morelos", "Nayarit", "Nuevo León", "Oaxaca", "Puebla", "Querétaro", 
           "Quintana Roo", "San Luis Potosí", "Sinaloa", "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala", 
           "Veracruz", "Yucatán", "Zacatecas")

# Paleta de colores
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

p <- chorddiag(tabla, 
               groupNames = names,
               groupColors = paleta, 
               groupnamePadding = 10, 
               #height = 700, 
               #width = 700,
               margin = 120,
               groupThickness = 0.07,
               groupPadding = 3,
               groupnameFontsize = 12,
               fadeLevel = '0.1',
               tickInterval = seq(0, 500000, 10000),
               chordedgeColor = "transparent",
               showZeroTooltips = FALSE,
               showTicks = TRUE)

# Ajusta las etiquetas usando JavaScript para modificar su posición
p <- htmlwidgets::onRender(p, '
      function(el, x) {
        d3.selectAll(".group text")
          .attr("text-anchor", "middle")
          .attr("dx", "0")  
          .attr("dy", "0.75em"); 
      }
')

# Crear un contenedor div y aplicar estilos CSS para centrarlo
#p <- tags$div(style = "display: flex; justify-content: center; align-items: center;", p)

p
p %>% 
 mapview::mapshot(url = paste0(here::here(),"/images/MR5a_2000.html"))

#htmlwidgets::saveWidget(p, paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2000/MR5a a nivel estatal 1995-2000.html"), selfcontained = TRUE)
#require(webshot)
#webshot(url = paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2000/MR5a a nivel estatal 1995-2000.html"),
 #         file = paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2000/MR5a a nivel estatal 1995-2000.png"),
  #                cliprect = "viewport")

Gráficos migración reciente

ChordDiagram

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2000/Matriz de migracion reciente a nivel estatal 1995-2000.RData"))

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0)  

rownames(tabla) <- stringr::str_wrap(nom_estados, 50)
colnames(tabla) <- stringr::str_wrap(nom_estados, 50)

# Paleta de colores
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

tabla2 <- color_chord_diagram(tabla1 = tabla, paleta)
file = "/Graficos/Estado/01_Migracion reciente/2000/ChordDiagram de MR5a a nivel estatal.pdf"

## Gráficos a nivel estatal 
chord_diagram_graph(file = file, 
                    width = 7, 
                    height = 7, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0.4,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Gráficos por estados

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2000/Matriz de migracion reciente a nivel estatal 1995-2000.RData"))

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0) 

rownames(tabla) <- stringr::str_wrap(nom_estados, 100)
colnames(tabla) <- stringr::str_wrap(nom_estados, 100)

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

filtro_est <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) 

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value > 0 & rn != estado[x]) %>%
#tabla_estados <- sapply(1:32, function(x)
 #                       mean(tail(sort(tabla1[[x]]), 1), na.rm = TRUE))
#p <- data.frame(estados = est,
 #               filtro_estados = filtro_mig)
#write.table(p, file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2000/Filtro a nivel estatal.txt"))
#write.xlsx(p, file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2000/Filtro a nivel estatal.xlsx"))

filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2000/Filtro a nivel estatal.xlsx"), colNames = TRUE) %>% 
               pull(filtro_estados)

tabla1 <- migration_flows_states(tabla = tabla, 
                                 filtro_mig = filtro_mig, 
                                 filtro_est = filtro_est, 
                                 category = estado, 
                                 group = "Otro estados")

## Se guardan las matrices de migración reciente para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
                                 name = "Total",,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2000/Matriz MR5a por estados_1995-2000_Reduccion.xlsx"), 
               overwrite = TRUE)
}
saveRDS(tabla1, file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2000/Tabla MR5a a por estados.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2000/Tabla MR5a a por estados.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_ENT", 
                        Inmigrantes = "Inmigrantes",  
                        Emigrantes = "Emigrantes")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_ENT", 
                                  Inmigrantes = "%Inmigrantes",  
                                  Emigrantes = "%Emigrantes")

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, total_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2000/Matriz MR5a por estados_1995-2000_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2000/Tabla MR5a a por estados.RDS"))

## Paleta de colores
#paleta <- colorRampPalette(pals::kovesi.linear_bmy_10_95_c78(100))(50)
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636")

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Estado/01_Migracion reciente/2000/ChordDiagram de MR5a desagregado por estado.pdf"

## Gráficos a nivel estatal 
chord_diagram_graph(file = file, 
                    width = 8, 
                    height = 8, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Estado/01_Migracion reciente/2000/Etiquetas a nivel estatal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Gráfico Sankey

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2000/Matriz de migracion reciente a nivel estatal 1995-2000.RData"))

Migrantes <- Migrantes %>%
              sna::diag.remove(remove.val = 0)  

rownames(Migrantes) <- stringr::str_wrap(nom_estados, 50)
colnames(Migrantes) <- stringr::str_wrap(nom_estados, 50)

# Matiz migración interna (Población de 5 años y más)
tabla <- Migrantes %>% 
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             as_tibble() %>%
              mutate(rn = forcats::fct_relevel(.$rn, nom_estados),
                     cn = forcats::fct_relevel(.$cn, nom_estados)) %>%
               filter(value >= 0)  
p <- tabla %>% 
       ggplot(aes(axis1 = rn, 
                   axis2 = cn, 
                    y = value),  # c("value", "freq", "tasa")
               reverse = FALSE, 
                na.rm = TRUE) +
        geom_alluvium(aes(fill = rn),
                       curve_type = "quintic", 
                        color = "transparent", 
                         alpha = 0.85, 
                          lwd = 0.001, 
                           width = 1/5,
                            reverse = FALSE) +
         geom_stratum(aes(fill = cn), 
                       color = "white", 
                        alpha = 0.65,  
                         lwd = 0.001, 
                          width = 1/5,
                           reverse = FALSE) +
          geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                               fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                           stat = "stratum", 
                            size = 3, 
                             direction = "y", 
                              nudge_x = -.3,
                               min.segment.length = unit(1, "lines"),
                                force = 1,
                                 force_pull = 0,
                                  family = "montserrat",
                                   reverse = FALSE) +
           geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                               fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                            stat = "stratum", 
                             size = 3,
                              direction = "y", 
                               nudge_x = .3, 
                                force = 1,
                                 force_pull = 0,
                                  family = "montserrat",
                                   reverse = FALSE) +
            theme_void() + 
             theme(plot.margin = margin(t = 1, r = 1, b = 1, l = 1, "cm"),
                    text = element_text(family = "montserrat"),
                     axis.text = element_blank(),
                      axis.title = element_blank(),
                       strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                        legend.key.size = unit(0.5, "cm"),
                         legend.text = element_text(size = 9, family = "montserrat")) + 
              scale_x_discrete(expand = c(0, 0.4)) +
               scale_fill_viridis_d(option = "A", end = 0.9, begin = 0.2) +
                guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                 labs(fill = "", 
                       color = "")
p
path = paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2000/GSankey de MR5a a nivel estatal.pdf")
ggexport(p, width = 14, height = 10, dpi = 400, filename = path)

Desagregado por estados

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2000/Matriz de migracion reciente a nivel estatal 1995-2000.RData"))

rownames(Migrantes) <- estados
colnames(Migrantes) <- estados

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0) %>%
           as.data.frame() %>%
            tibble::rownames_to_column(var = "rn") %>% 
             melt(., id.vars = "rn", variable.name = "cn") %>%
              mutate_if(is.factor, as.character) %>%
               mutate(value = ifelse((.$rn != .$cn) & (.$rn %in% estados | .$cn %in% estados), value, 0)) %>%
                mutate(rn = forcats::fct_relevel(.$rn, estados),
                       cn = forcats::fct_relevel(.$cn, estados)) 
p <- lapply(1:32, function(x){
                   tabla <- tabla %>%
                             mutate(rn = forcats::fct_relevel(.$rn, estados),
                                    cn = forcats::fct_relevel(.$cn, estados)) %>%
                              mutate(value = ifelse(.$rn %in% estados[x] | .$cn %in% estados[x], value, 0)) 

                    tabla %>% 
                     ggplot(aes(axis1 = rn, 
                                 axis2 = cn, 
                                  y = value),  # c("value", "freq", "tasa")
                             reverse = FALSE, 
                              na.rm = TRUE) + 
                      geom_alluvium(aes(fill = rn),  
                                     color = "transparent", 
                                      alpha = 0.8, 
                                       lwd = 0.001, 
                                        width = 1/5,
                                         reverse = FALSE) +
                       geom_stratum(aes(fill = rn), 
                                     color = "#F1F1F1", 
                                      alpha = 1, 
                                       lwd = 0.001, 
                                        width = 1/5,
                                         reverse = FALSE) +
                         geom_text_repel(aes(label = ifelse(after_stat(x)  == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                             fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                           stat = "stratum", 
                                            size = 3,
                                             direction = "y", 
                                              nudge_x = -.2, 
                                               force = 1,
                                                        force_pull = 0,
                                                         family = "montserrat",
                                                          reverse = FALSE) +
                          geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                              fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                           stat = "stratum", 
                                            size = 3,
                                             direction = "y", 
                                              nudge_x = .2, 
                                               force = 1,
                                                force_pull = 0,
                                                 family = "montserrat",
                                                  reverse = FALSE) +
                            theme_void() + 
                             theme(plot.margin = margin(t = 1, r = 1.5, b = 1, l = 0, "cm"),
                                    text = element_text(family = "montserrat"),
                                     axis.text = element_blank(),
                                      axis.title = element_blank(),
                                       strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                        legend.key.size = unit(0.5, "cm"),
                                         legend.text = element_text(size = 9, family = "montserrat"),
                                          legend.position = c(1, .5)) + 
                              scale_x_discrete(expand = c(-0.1, 0.35)) +
                               scale_fill_viridis_d(option = "A", end = 0.9, begin = 0.2) +
                                guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                                 labs(fill = "", 
                                      color = "")
              }
        )

path = paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2000/GSankey de MR5a desagregado por estados_Absolutos.pdf")
ggexport(list = p, width = 14, height = 10, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de migración

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_ENT) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <=130) %>%
                 group_by(CVE_ENT) %>%
                  summarise(Pob.5ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2000/Matriz de migracion reciente a nivel estatal 1995-2000.RData"))

rownames(Migrantes) <- estados
colnames(Migrantes) <- estados
  
Residentes <- Migrantes %>%
               rownames_to_column() %>%
                tidyr::gather(CVE_ENT, Value, -rowname)%>%
                 filter(rowname == CVE_ENT) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>% 
                sna::diag.remove(remove.val = 0) %>%
                 as.data.frame() %>%
                  tibble::rownames_to_column(var = "CVE_ENT") %>%
                   melt(., id.vars = "CVE_ENT", variable.name = "RES95EDO_C") %>%
                    mutate_at(vars(3), as.numeric) %>%
                     as_tibble() %>%
                      filter(CVE_ENT != RES95EDO_C) %>%
                       group_by(CVE_ENT) %>%
                        summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>% 
               sna::diag.remove(remove.val = 0) %>%
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_ENT") %>%
                  melt(., id.vars = "CVE_ENT", variable.name = "RES95EDO_C") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_ENT != RES95EDO_C) %>%
                      group_by(RES95EDO_C) %>%
                       summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                        rename("CVE_ENT" = "RES95EDO_C") 

tabla <- Pob.Total %>%
          left_join(., Pob.5ymas, by = c("CVE_ENT")) %>%
          left_join(., Residentes, by = c("CVE_ENT")) %>%
          left_join(., Inmigrantes, by = c("CVE_ENT")) %>%
          left_join(., Emigrantes, by = c("CVE_ENT")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2))*1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2))*1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2000/Indicadores de MR5a por estado 1995-2000.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Indicadores de MR5a por estado 1995-2000.RData"))
Indicadores de migración reciente
Nivel estatal
CVE_ENT Pob.Total Pob.5ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001 940 778 822 037 772 422 42 307 21 046 21 261 63 353 9.6 4.8 4.8 −42 092
002 2 476 010 2 181 179 1 885 490 250 467 69 548 180 919 320 015 21.5 6.0 15.5 −139 096
003 418 962 372 466 332 660 36 537 17 664 18 873 54 201 18.5 8.9 9.5 −35 328
004 687 572 609 648 574 209 33 630 26 734 6 896 60 364 10.4 8.2 2.1 −53 468
005 2 287 816 2 031 773 1 945 192 73 354 71 045 2 309 144 399 6.8 6.6 0.2 −142 090
006 536 650 479 149 441 228 31 698 22 971 8 727 54 669 12.5 9.0 3.4 −45 942
007 3 912 081 3 393 756 3 324 721 46 563 94 966 −48 403 141 529 2.5 5.2 −2.7 −189 932
008 3 037 366 2 683 956 2 500 018 154 959 51 075 103 884 206 034 10.8 3.6 7.3 −102 150
009 8 550 170 7 787 688 7 335 085 406 388 847 059 −440 671 1 253 447 9.9 20.7 −10.8 −1 694 118
010 1 440 899 1 266 521 1 214 838 38 737 72 817 −34 080 111 554 5.7 10.8 −5.0 −145 634
011 4 648 460 4 064 431 3 926 705 97 464 76 546 20 918 174 010 4.5 3.5 1.0 −153 092
012 3 063 380 2 656 187 2 579 674 53 986 139 912 −85 926 193 898 3.8 9.8 −6.0 −279 824
013 2 226 763 1 974 004 1 864 655 97 556 82 969 14 587 180 525 9.3 7.9 1.4 −165 938
014 6 293 460 5 567 957 5 341 337 158 634 151 340 7 294 309 974 5.3 5.1 0.2 −302 680
015 13 058 570 11 611 426 10 789 780 766 164 467 062 299 102 1 233 226 12.4 7.6 4.8 −934 124
016 3 959 772 3 487 151 3 346 484 92 065 114 516 −22 451 206 581 4.9 6.2 −1.2 −229 032
017 1 545 775 1 373 243 1 273 480 87 502 50 506 36 996 138 008 12.0 6.9 5.1 −101 012
018 910 241 809 050 764 879 33 990 41 122 −7 132 75 112 7.9 9.6 −1.7 −82 244
019 3 812 758 3 405 823 3 250 584 135 187 71 622 63 565 206 809 7.5 4.0 3.5 −143 244
020 3 419 524 3 006 497 2 906 358 80 755 144 822 −64 067 225 577 5.0 9.0 −4.0 −289 644
021 5 054 788 4 440 642 4 277 152 135 656 160 988 −25 332 296 644 5.7 6.8 −1.1 −321 976
022 1 398 148 1 230 278 1 142 717 79 711 35 870 43 841 115 581 12.1 5.5 6.7 −71 740
023 870 918 762 866 633 159 123 546 37 568 85 978 161 114 30.2 9.2 21.1 −75 136
024 2 290 332 2 011 828 1 940 412 55 534 75 604 −20 070 131 138 5.2 7.0 −1.9 −151 208
025 2 522 862 2 239 357 2 139 710 86 708 126 390 −39 682 213 098 7.3 10.6 −3.3 −252 780
026 2 192 455 1 945 692 1 852 115 77 331 61 377 15 954 138 708 7.5 5.9 1.5 −122 754
027 1 883 620 1 660 813 1 611 285 44 529 72 501 −27 972 117 030 5.0 8.2 −3.2 −145 002
028 2 735 624 2 428 828 2 240 017 169 560 74 039 95 521 243 599 13.1 5.7 7.4 −148 078
029 957 705 844 878 798 486 42 999 28 332 14 667 71 331 9.5 6.3 3.3 −56 664
030 6 883 273 6 121 833 5 927 396 171 515 385 207 −213 692 556 722 5.3 11.8 −6.6 −770 414
031 1 650 949 1 476 223 1 426 618 43 950 43 303 647 87 253 5.6 5.5 0.1 −86 606
032 1 347 186 1 184 735 1 131 817 35 341 47 802 −12 461 83 143 5.6 7.6 −2.0 −95 604
Fuente: Estimaciones del CONAPO.

Nivel municipal

Migración reciente

Matrices

#Clave de los municipios 2000 
municipios <- MUN %>%
               select(CVE_MUN) %>%
                unique() %>%
                 pull(CVE_MUN)

Pob.5ymas <- mydata %>%
              mutate(CVE_MUN_RES = paste0(RES95EDO_C, MUN95OTR_C)) %>%
               mutate(RES95EDO_C = case_when(.$RES95EDO_C %in% estados ~.$RES95EDO_C,
                                             .$RES95EDO_C %nin% estados ~ "888", #Residencia en otro país
                                             .$RES95EDO_C %in% "997" ~ "997",
                                             .$RES95EDO_C %in% "998" ~ "998",
                                             .$RES95EDO_C %in% "997" ~ "999"),
                      CVE_MUN_RES = case_when(.$CVE_MUN_RES %in% municipios ~.$CVE_MUN_RES,
                                               nchar(.$CVE_MUN_RES) == 3 ~ "888",  #Residencia en otro país
                                              .$CVE_MUN_RES %in% "997999" ~ '997999',
                                              .$RES95EDO_C %in% "997" ~ "997",
                                              .$RES95EDO_C %in% "998" ~ "998",
                                              .$RES95EDO_C %in% "999" ~ "999",
                                              .$MUN95OTR_C %in% "999" ~ "999")) %>%
                 mutate(EDAD = as.numeric(.$EDAD)) %>%
                  subset(EDAD >= 5 & EDAD <= 130) %>%
                   select(FACTOR, ESTRATO, UPM, CVE_MUN, CVE_MUN_RES, EDAD) %>%
                    filter(CVE_MUN_RES %in% municipios) 

MC <- Pob.5ymas %>%
       svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/MC_municipal.RDS"))
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/MC_municipal.RDS"))

Migrantes <- svytable(~CVE_MUN_RES + CVE_MUN, design = MC) 

Se genera la matriz cuadrada y se le asignan los nombres de los estados.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_RES, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 

rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 20)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel municipal.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel municipal.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Reciente")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel municipal.xlsx"), overwrite = TRUE)

Matriz de migración reciente hace 5 años a nivel municipal, 1995-2000

Matriz de migración reciente 1995-2000
Nivel municipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 520613 490 723 66 311 328 266 13 100 157 153 0 142 8 301 0 0 0 110 41 0 0 0 0 0 0 0 0 0
001002 128 30171 7 0 0 9 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 93 9 41234 0 0 0 0 0 4 0 0 0 11 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001004 43 3 0 10220 0 0 56 0 0 0 63 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 2177 0 95 0 49532 6 26 104 104 26 443 0 0 0 57 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 236 25 0 0 29 28155 106 136 118 0 28 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 116 0 173 26 2 173 33530 65 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 16 0 18 0 0 0 10 6053 0 0 0 12 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 59 4 0 2 0 0 24 0 14006 36 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 110 15 0 0 0 0 0 0 0 12473 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001011 508 110 0 0 46 159 19 0 6 20 15369 0 0 0 28 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002001 77 23 0 0 52 0 0 0 0 0 0 280807 1384 217 1832 108 284 466 353 58 28 0 37 0 0 0 0 0 0
002002 152 23 60 0 0 0 0 0 0 0 0 1129 616691 337 1294 0 52 49 267 45 26 0 19 0 0 0 0 0 0
002003 0 0 0 0 0 0 0 0 0 0 0 154 497 54801 800 0 0 0 0 228 0 0 0 0 0 0 0 0 0
002004 435 0 156 0 15 0 1 0 0 0 0 2978 3054 301 861641 136 0 132 1035 83 19 0 36 0 38 0 0 0 0
002005 0 0 0 0 0 0 0 0 0 0 0 257 405 144 1513 42760 0 144 0 0 0 0 0 26 0 0 0 0 0
003001 0 0 0 0 0 0 0 0 0 0 0 62 0 0 127 0 53172 189 344 118 23 0 0 45 0 0 0 0 0
003002 18 0 0 0 0 0 0 0 0 0 0 485 61 18 104 33 326 34917 162 77 146 0 0 0 0 0 0 0 0
003003 12 0 0 0 0 0 0 0 0 0 0 528 113 0 430 28 1759 517 157926 847 75 0 0 33 0 0 0 0 0
003008 12 0 0 0 0 0 0 0 0 0 0 123 200 58 332 0 1146 37 2743 67921 173 0 0 0 0 0 0 0 0
003009 1 0 0 0 0 0 0 0 0 0 0 63 16 9 16 0 308 67 184 14 8711 0 0 0 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 40837 47 13 52 40 0 0 24
004002 0 0 0 0 0 0 0 0 0 0 0 18 0 0 37 0 0 0 0 0 0 180 180546 1207 979 470 583 136 62
004003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 121 372 134808 149 18 0 103 18
004004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 46 407 97 58542 0 12 0 0
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 82 36 6 83 21955 3 0 16
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 184 20 109 4 26906 0 0
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 43 90 4 0 4 6982 0
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 22 75 15 8 0 0 0 7213
004009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 114 0 0 0 0 0 0 154 11 728 1640 0 106 0 0
Fuente: Estimaciones del CONAPO.

Gráficos

Gráficos por municipios

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel municipal.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

municipios <- MUN %>% 
               mutate(municipios = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                pull(municipios)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(municipios, Migrantes)

Emigrantes <- Emigrantes_function(municipios, Migrantes)   

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE))

filtro_est <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   filter(value > 0)

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value > 0 & rn != estado[x]) %>%
#### Se anexa el filtro de estados filter(value > 100000000 & rn != estado[x]) %>% 
#### De esta manera solo se contemplan a los municipios

#p <- data.frame(estados = est,
 #               filtro_municipio = filtro_mig,
  #              filtro_estado = filtro_out)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2000/Filtro a nivel municipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2000/Filtro a nivel municipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

#### Filtro de estados 
filtro_out <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2000/Filtro a nivel municipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_estado)

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              filter(value > 0)

## Se generan los filtros correspondientes a la matriz cuadrada por estados 
tabla1 <- migration_flows_municipality(tabla = tabla, 
                                       filtro_mun = filtro,
                                       filtro_est = filtro_est,
                                       filtro_mig = filtro_mig, 
                                       filtro_out = filtro_out, 
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group_mun = "Otros municipios",
                                       group_est = "Otros estados")

## Se sacan los flujos migratorios que pertencen a otros estados
#tabla_estados <- sapply(1:32, function(i){
#                         tabla1[[i]] %>%
#                          as.data.frame() %>%
#                           adorn_totals(c("row", "col"), 
#                                         fill = "-", 
#                                          na.rm = TRUE, 
#                                           ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                            select(`Otros estados`) %>%
#                             slice(nrow(.)) %>%
#                              mutate(`Otros estados` = .$`Otros estados`/30) %>%
#                               pull(`Otros estados`)
#  }
#)

## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:32, function(i){
#                               tabla1[[i]] %>%
#                                as.data.frame() %>%
#                                 select(-c(`Otros estados`)) %>%
#                                  adorn_totals(c("row", "col"), 
#                                                fill = "-", 
#                                                 na.rm = TRUE, 
#                                                  ,,,,contains(colnames(tabla1[[i]])))  N%>% 
#                                    slice(nrow(.)) %>%
#                                     mutate(Total = .$Total/50) %>%
#                                      pull(Total)
#})
## Se guardan las matrices de migración reciente para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz MR5a nivel municipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}

saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Tabla MR5a a nivel municipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Tabla MR5a a nivel municipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Inmigrantes",  
                        Emigrantes = "Emigrantes")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Inmigrantes",  
                                  Emigrantes = "%Emigrantes")
# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, total_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz MR5a nivel municipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}

Dada la magnitud de municipios en algunos estados se seleccionan solo algunos de ellos.

tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Tabla MR5a a nivel municipal.RDS"))

## Paleta de colores
#paleta <- colorRampPalette(pals::kovesi.linear_bmy_10_95_c78(100))(50)
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Municipio/01_Migracion reciente/2000/ChordDiagram de MR5a desagregado por estado.pdf"

## Gráficos a nivel municipal
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file =  "/Graficos/Municipio/01_Migracion reciente/2000/Etiquetas a nivel municipal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 10, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Indicadores

Se realizan cálculos generales de migración

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <=130) %>%
                 group_by(CVE_MUN) %>%
                  summarise(Pob.5ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel municipal.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>% 
                gather(CVE_MUN, Value, -rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_RES) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_RES) %>%
                     group_by(CVE_MUN_RES) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_RES") 

tabla <- Pob.Total %>%
          left_join(., Pob.5ymas, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
            mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                   Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                   Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2))*1000,
                   Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2))*1000,
                   Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                   Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Indicadores de migracion reciente a nivel municipal 1995-2000.xlsx"), overwrite = TRUE)

save(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Indicadores de migracion reciente a nivel municipal 1995-2000.RData"))
Indicadores de migración reciente
Nivel municipal
CVE_MUN Pob.Total Pob.5ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 641 132 565 045 520 613 32 530 18 843 13 687 51 373 10.8 6.2 4.5 −37 686
001002 37 617 32 042 30 171 1 177 873 304 2 050 6.8 5.0 1.7 −1 746
001003 51 200 43 800 41 234 1 198 1 871 −673 3 069 5.0 7.9 −2.8 −3 742
001004 12 590 10 834 10 220 400 192 208 592 6.8 3.3 3.6 −384
001005 63 884 54 820 49 532 4 559 741 3 818 5 300 15.4 2.5 12.9 −1 482
001006 34 123 29 923 28 155 1 386 1 037 349 2 423 8.7 6.5 2.2 −2 074
001007 41 501 35 055 33 530 1 107 903 204 2 010 5.8 4.7 1.1 −1 806
001008 7 246 6 263 6 053 103 593 −490 696 3.0 17.6 −14.5 −1 186
001009 16 479 14 352 14 006 203 504 −301 707 2.6 6.5 −3.9 −1 008
001010 15 027 12 845 12 473 224 557 −333 781 3.2 8.0 −4.8 −1 114
001011 19 979 17 058 15 369 1 358 844 514 2 202 14.7 9.1 5.6 −1 688
002001 367 281 323 690 280 807 31 317 15 560 15 757 46 877 18.1 9.0 9.1 −31 120
002002 761 169 678 495 616 691 39 134 17 848 21 286 56 982 10.9 5.0 5.9 −35 696
002003 77 011 67 014 54 801 9 174 2 996 6 178 12 170 25.5 8.3 17.2 −5 992
002004 1 207 045 1 056 227 861 641 134 778 39 035 95 743 173 813 23.8 6.9 16.9 −78 070
002005 63 504 55 753 42 760 9 545 1 589 7 956 11 134 32.0 5.3 26.7 −3 178
003001 63 021 55 998 53 172 2 118 4 752 −2 634 6 870 7.1 16.0 −8.9 −9 504
003002 45 135 39 813 34 917 3 454 2 384 1 070 5 838 16.3 11.2 5.0 −4 768
003003 194 714 174 999 157 926 14 093 12 475 1 618 26 568 15.2 13.5 1.8 −24 950
003008 104 665 91 462 67 921 19 885 4 193 15 692 24 078 40.6 8.6 32.0 −8 386
Fuente: Estimaciones del CONAPO.

Migración Intramunicipal

Matrices

#Clave de los municipios 2000 
municipios <- MUN %>%
               select(CVE_MUN) %>%
                unique() %>%
                 pull(CVE_MUN)

Pob.5ymas <- mydata %>%
              mutate(CVE_MUN_RES = paste0(RES95EDO_C, MUN95OTR_C)) %>%
               mutate(RES95EDO_C = case_when(.$RES95EDO_C %in% estados ~.$RES95EDO_C,
                                             .$RES95EDO_C %nin% estados ~ "888", #Residencia en otro país
                                             .$RES95EDO_C %in% "997" ~ "997",
                                             .$RES95EDO_C %in% "998" ~ "998",
                                             .$RES95EDO_C %in% "997" ~ "999"),
                      CVE_MUN_RES = case_when(.$CVE_MUN_RES %in% municipios ~.$CVE_MUN_RES,
                                               nchar(.$CVE_MUN_RES) == 3 ~ "888",  #Residencia en otro país
                                              .$CVE_MUN_RES %in% "997999" ~ '997999',
                                              .$RES95EDO_C %in% "997" ~ "997",
                                              .$RES95EDO_C %in% "998" ~ "998",
                                              .$RES95EDO_C %in% "999" ~ "999",
                                              .$MUN95OTR_C %in% "999" ~ "999")) %>%
                 mutate(I_Migracion = case_when(.$CVE_ENT == .$RES95EDO_C & .$RES95EDO_C %in% estados ~ 1,
                                                .$CVE_ENT != .$RES95EDO_C & .$RES95EDO_C %in% estados ~ 2,
                                                .$RES95EDO_C %nin% estados ~ 3)) %>%
                  mutate(EDAD = as.numeric(.$EDAD)) %>%
                   subset(EDAD >= 5 & EDAD <= 130) %>%
                    select(FACTOR, ESTRATO, UPM, CVE_MUN, CVE_MUN_RES, EDAD, I_Migracion) %>%
                     filter(CVE_MUN_RES %in% municipios & I_Migracion == 1) 

MC <- Pob.5ymas %>%
       svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/MC_intramunicipal.RDS"))
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/MC_intramunicipal.RDS"))

Migrantes <- svytable(~CVE_MUN_RES + CVE_MUN, design = MC) 

Se genera la matriz cuadrada y se le asignan las etiquetas de municipios.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_RES, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 
            
rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 20)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel intramunicipal 2000.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel intramunicipal 2000.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Intramunicipal")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel intramunicipal 2000.xlsx"), overwrite = TRUE)

Matriz de migración reciente hace 5 años a nivel municipal, 1995-2000

Matriz de migración reciente a nivel municipal
Nivel intramunicipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 520613 490 723 66 311 328 266 13 100 157 153 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001002 128 30171 7 0 0 9 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 93 9 41234 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001004 43 3 0 10220 0 0 56 0 0 0 63 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 2177 0 95 0 49532 6 26 104 104 26 443 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 236 25 0 0 29 28155 106 136 118 0 28 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 116 0 173 26 2 173 33530 65 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 16 0 18 0 0 0 10 6053 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 59 4 0 2 0 0 24 0 14006 36 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 110 15 0 0 0 0 0 0 0 12473 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001011 508 110 0 0 46 159 19 0 6 20 15369 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002001 0 0 0 0 0 0 0 0 0 0 0 280807 1384 217 1832 108 0 0 0 0 0 0 0 0 0 0 0 0 0
002002 0 0 0 0 0 0 0 0 0 0 0 1129 616691 337 1294 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002003 0 0 0 0 0 0 0 0 0 0 0 154 497 54801 800 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002004 0 0 0 0 0 0 0 0 0 0 0 2978 3054 301 861641 136 0 0 0 0 0 0 0 0 0 0 0 0 0
002005 0 0 0 0 0 0 0 0 0 0 0 257 405 144 1513 42760 0 0 0 0 0 0 0 0 0 0 0 0 0
003001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 53172 189 344 118 23 0 0 0 0 0 0 0 0
003002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 326 34917 162 77 146 0 0 0 0 0 0 0 0
003003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1759 517 157926 847 75 0 0 0 0 0 0 0 0
003008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1146 37 2743 67921 173 0 0 0 0 0 0 0 0
003009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 308 67 184 14 8711 0 0 0 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 40837 47 13 52 40 0 0 24
004002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 180 180546 1207 979 470 583 136 62
004003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 121 372 134808 149 18 0 103 18
004004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 46 407 97 58542 0 12 0 0
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 82 36 6 83 21955 3 0 16
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 184 20 109 4 26906 0 0
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 43 90 4 0 4 6982 0
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 22 75 15 8 0 0 0 7213
004009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 154 11 728 1640 0 106 0 0
Fuente: Estimaciones del CONAPO.

Gráficos

ChordDiagram

Gráficos por estados

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel intramunicipal 2000.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

# Clave de los municipios 
municipios <- MUN %>% 
               mutate(municipios = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                pull(municipios)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(municipios, Migrantes)

Emigrantes <- Emigrantes_function(municipios, Migrantes)       

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value > 0 & rn != estado[x]) %>%
#p <- data.frame(estados = est,
 #               filtro_municipio = tabla_municipios)
#write.table(p, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2000/Filtro a nivel intramunicipal.txt"), col.names = TRUE)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2000/Filtro a nivel intramunicipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2000/Filtro a nivel intramunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

################################################################################
## Se generan los filtros correspondientes a la matriz cuadrada por estados 
tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              filter(value > 0)

tabla1 <- migration_flows_municipality(tabla = tabla, 
                                       filtro_mun = filtro,
                                       filtro_est = NULL,
                                       filtro_mig = filtro_mig, 
                                       filtro_out = NULL,
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group_mun = "Otros municipios",
                                       group_est = NULL)

################################################################################
## Se generan los filtros correspondientes a la matriz cuadrada por estados 
## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:32, function(i){
#                                  tabla1[[i]] %>%
#                                   as.data.frame() %>%
#                                    adorn_totals(c("row", "col"), 
#                                                  fill = "-", 
#                                                   na.rm = TRUE, 
#                                                    ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                    select(Total) %>%
#                                     slice(nrow(.)) %>%
#                                      mutate(Total = .$Total/100) %>%
#                                       pull(Total)
#}
#)
## Se guardan las matrices de migración reciente para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz MR5a nivel intramunicipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}
 
saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Tabla MR5a a nivel intramunicipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Tabla MR5a a nivel intramunicipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Inmigrantes",  
                        Emigrantes = "Emigrantes")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Inmigrantes",  
                                  Emigrantes = "%Emigrantes")

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, totales_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz MR5a nivel intramunicipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}

Dada la magnitud de municipios en algunos estados se seleccionan solo algunos de ellos.

tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Tabla MR5a a nivel intramunicipal.RDS"))

## Paleta de colores
#paleta <- colorRampPalette(pals::kovesi.linear_bmy_10_95_c78(100))(50)
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Municipio/01_Migracion reciente/2000/ChordDiagram de MR5a desagregado a nivel intramunicipal.pdf"

## Gráficos a nivel intramunicipal
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Municipio/01_Migracion reciente/2000/Etiquetas a nivel intramunicipal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Gráfico Sankey

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel intramunicipal 2000.RData"))

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character)

################################################################################
################################## Filtro ######################################
Inmigrantes  <- Migrantes %>%
                 as.data.frame() %>%
                  tibble::rownames_to_column(var = "rn") %>% 
                   melt(., id.vars = "rn", variable.name = "cn") %>%
                    mutate_if(is.factor, as.character) %>%
                     mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                      filter(value > 0) %>%
                       group_by(rn) %>% 
                        summarise(Inmigrantes = sum(value, na.rm = TRUE)) 

Emigrantes <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                    filter(value > 0) %>%
                     group_by(cn) %>% 
                      summarise(Emigrantes = sum(value, na.rm = TRUE))     

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value < 5000) %>% 
               pull(rn)
################################################################################

tabla1 <- lapply(1:32, function(x){
                   tabla %>%
                    mutate(rn = case_when(substr(.$rn, 1, 3) %in% estados[x] & .$rn %in% filtro ~ paste0("Otros municipios (", estados[x], ")"),
                                          substr(.$rn, 1, 3) %in% estados[x]  & .$rn %nin% filtro ~ .$rn,
                                          substr(.$rn, 1, 3) %nin% estados[x] ~ substr(.$rn, 1, 3)),
                           cn = case_when(substr(.$cn, 1, 3) %in% estados[x] & .$cn %in% filtro ~ paste0("Otros municipios (", estados[x], ")"),
                                          substr(.$cn, 1, 3) %in% estados[x] & .$cn %nin% filtro ~ .$cn,
                                          substr(.$cn, 1, 3) %nin% estados[x] ~ substr(.$cn, 1, 3))) %>%

                     mutate(value = ifelse(.$rn != .$cn, .$value, 0)) %>%
                      filter(value > 0) 
  }
)
p <- lapply(1:32, function(x){
             tabla1[[x]] %>% 
              ggplot(aes(axis1 = rn, 
                          axis2 = cn, 
                           y = value),  # c("value", "freq", "tasa")
                      reverse = FALSE, 
                       na.rm = TRUE) +
               geom_alluvium(aes(fill = rn),
                              curve_type = "quintic", 
                               color = "transparent", 
                                alpha = 0.85, 
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                 geom_stratum(aes(fill = cn), 
                               color = "white", 
                                alpha = 0.65,  
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                  geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                                       fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                   stat = "stratum", 
                                    size = 3, 
                                     direction = "y", 
                                      nudge_x = -.2,
                                       min.segment.length = unit(1, "lines"),
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                   geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                        fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                    stat = "stratum", 
                                     size = 3,
                                      direction = "y", 
                                       nudge_x = .2, 
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                     theme_void() + 
                      theme(plot.margin = margin(t = 1, r = 5, b = 1, l = 0, "cm"),
                             text = element_text(family = "montserrat"),
                              axis.text = element_blank(),
                               axis.title = element_blank(),
                                strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                 legend.key.size = unit(0.5, "cm"),
                                  legend.text = element_text(size = 7, family = "montserrat"),
                                   legend.position = c(1.02, .5)) + 
                       scale_x_discrete(expand = c(-0.1, 0.5)) +
                        scale_fill_viridis_d(option = "A", end = 1, begin = 0.2) +
                         guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                          labs(fill = "", 
                               color = "")
  }
)

path = paste0(here::here(), "/Graficos/Municipio/01_Migracion reciente/2000/GSankey de MR5a desagregado a nivel intramunicipal.pdf")
ggexport(list = p, width = 18, height = 10, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de migración

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <=130) %>%
                 group_by(CVE_MUN) %>%
                  summarise(Pob.5ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel intramunicipal 2000.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>%
                gather(CVE_MUN, Value, -rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_RES) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_RES) %>%
                     group_by(CVE_MUN_RES) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_RES") 

tabla <- Pob.Total %>%
          left_join(., Pob.5ymas, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Indicadores de MR5a a nivel intramunicipal 2000.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Indicadores de MR5a a nivel intramunicipal 2000.RData"))
Indicadores de migración reciente
Nivel intramunicipal
CVE_MUN Pob.Total Pob.5ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 641 132 565 045 520 613 2 607 3 486 −879 6 093 0.86 1.16 −0.3 −6 972
001002 37 617 32 042 30 171 153 656 −503 809 0.88 3.77 −2.9 −1 312
001003 51 200 43 800 41 234 106 1 016 −910 1 122 0.45 4.28 −3.8 −2 032
001004 12 590 10 834 10 220 165 94 71 259 2.82 1.61 1.2 −188
001005 63 884 54 820 49 532 2 981 388 2 593 3 369 10.05 1.31 8.7 −776
001006 34 123 29 923 28 155 678 675 3 1 353 4.23 4.22 0.0 −1 350
001007 41 501 35 055 33 530 557 516 41 1 073 2.91 2.70 0.2 −1 032
001008 7 246 6 263 6 053 44 318 −274 362 1.30 9.42 −8.1 −636
001009 16 479 14 352 14 006 125 332 −207 457 1.62 4.31 −2.7 −664
001010 15 027 12 845 12 473 125 241 −116 366 1.79 3.46 −1.7 −482
001011 19 979 17 058 15 369 868 687 181 1 555 9.37 7.42 2.0 −1 374
002001 367 281 323 690 280 807 3 541 4 518 −977 8 059 2.05 2.62 −0.6 −9 036
002002 761 169 678 495 616 691 2 760 5 340 −2 580 8 100 0.77 1.48 −0.7 −10 680
002003 77 011 67 014 54 801 1 451 999 452 2 450 4.03 2.77 1.3 −1 998
002004 1 207 045 1 056 227 861 641 6 469 5 439 1 030 11 908 1.14 0.96 0.2 −10 878
002005 63 504 55 753 42 760 2 319 244 2 075 2 563 7.78 0.82 7.0 −488
003001 63 021 55 998 53 172 674 3 539 −2 865 4 213 2.27 11.89 −9.6 −7 078
003002 45 135 39 813 34 917 711 810 −99 1 521 3.35 3.81 −0.5 −1 620
003003 194 714 174 999 157 926 3 198 3 433 −235 6 631 3.46 3.71 −0.3 −6 866
003008 104 665 91 462 67 921 4 099 1 056 3 043 5 155 8.36 2.15 6.2 −2 112
Fuente: Estimaciones del CONAPO.

Migración Intermunicipal

Matrices

#Clave de los municipios 2000 
municipios <- MUN %>%
               select(CVE_MUN) %>%
                unique() %>%
                 pull(CVE_MUN)

Pob.5ymas <- mydata %>%
              mutate(CVE_MUN_RES = paste0(RES95EDO_C, MUN95OTR_C)) %>%
               mutate(RES95EDO_C = case_when(.$RES95EDO_C %in% estados ~.$RES95EDO_C,
                                             .$RES95EDO_C %nin% estados ~ "888", #Residencia en otro país
                                             .$RES95EDO_C %in% "997" ~ "997",
                                             .$RES95EDO_C %in% "998" ~ "998",
                                             .$RES95EDO_C %in% "997" ~ "999"),
                      CVE_MUN_RES = case_when(.$CVE_MUN_RES %in% municipios ~.$CVE_MUN_RES,
                                               nchar(.$CVE_MUN_RES) == 3 ~ "888",  #Residencia en otro país
                                              .$CVE_MUN_RES %in% "997999" ~ '997999',
                                              .$RES95EDO_C %in% "997" ~ "997",
                                              .$RES95EDO_C %in% "998" ~ "998",
                                              .$RES95EDO_C %in% "999" ~ "999",
                                              .$MUN95OTR_C %in% "999" ~ "999")) %>%
                 mutate(I_Migracion = case_when(.$CVE_ENT == .$RES95EDO_C & .$RES95EDO_C %in% estados ~ 1,
                                                .$CVE_ENT != .$RES95EDO_C & .$RES95EDO_C %in% estados ~ 2,
                                                .$RES95EDO_C %nin% estados ~ 3)) %>%
                  mutate(EDAD = as.numeric(.$EDAD)) %>%
                   subset(EDAD >= 5 & EDAD <= 130) %>%
                    select(FACTOR, ESTRATO, UPM, CVE_MUN, CVE_MUN_RES, EDAD, I_Migracion) %>%
                     filter(CVE_MUN_RES %in% municipios & I_Migracion == 2) 

MC <- Pob.5ymas %>%
       svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/MC_intermunicipal.RDS"))
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/MC_intermunicipal.RDS"))

Migrantes <- svytable(~CVE_MUN_RES + CVE_MUN, design = MC) 

Se genera la matriz cuadrada y se le asignan las etiquetas de municipios.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_RES, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 
            
rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 20)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel intermunicipal 2000.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel intermunicipal 2000.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Intermunicipal")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel intermunicipal 2000.xlsx"), overwrite = TRUE)

Matriz de migración reciente hace 5 años a nivel municipal, 1995-2000

Matriz de migración reciente a nivel municipal
Nivel intermunicipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 0 0 0 0 0 0 0 0 0 0 0 0 142 8 301 0 0 0 110 41 0 0 0 0 0 0 0 0 0
001002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 0 0 0 0 0 0 0 0 0 0 0 0 11 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 57 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 0 0 0 0 0 0 0 0 0 0 0 12 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001011 0 0 0 0 0 0 0 0 0 0 0 0 0 0 28 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002001 77 23 0 0 52 0 0 0 0 0 0 0 0 0 0 0 284 466 353 58 28 0 37 0 0 0 0 0 0
002002 152 23 60 0 0 0 0 0 0 0 0 0 0 0 0 0 52 49 267 45 26 0 19 0 0 0 0 0 0
002003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 228 0 0 0 0 0 0 0 0 0
002004 435 0 156 0 15 0 1 0 0 0 0 0 0 0 0 0 0 132 1035 83 19 0 36 0 38 0 0 0 0
002005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 144 0 0 0 0 0 26 0 0 0 0 0
003001 0 0 0 0 0 0 0 0 0 0 0 62 0 0 127 0 0 0 0 0 0 0 0 45 0 0 0 0 0
003002 18 0 0 0 0 0 0 0 0 0 0 485 61 18 104 33 0 0 0 0 0 0 0 0 0 0 0 0 0
003003 12 0 0 0 0 0 0 0 0 0 0 528 113 0 430 28 0 0 0 0 0 0 0 33 0 0 0 0 0
003008 12 0 0 0 0 0 0 0 0 0 0 123 200 58 332 0 0 0 0 0 0 0 0 0 0 0 0 0 0
003009 1 0 0 0 0 0 0 0 0 0 0 63 16 9 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004002 0 0 0 0 0 0 0 0 0 0 0 18 0 0 37 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 114 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Fuente: Estimaciones del CONAPO.

Gráficos

ChordDiagram

Gráficos por estados

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel intermunicipal 2000.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

# Clave de los municipios 
municipios <- MUN %>% 
               mutate(municipios = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                pull(municipios)
             
################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(municipios, Migrantes)

Emigrantes <- Emigrantes_function(municipios, Migrantes)       

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value > 0)

filtro_est <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   filter(value > 0)

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value > 0 & rn != estado[x]) %>%
#p <- data.frame(estados = est,
 #               filtro_municipio = tabla_municipios,
  #              filtro_estado = tabla_estados)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2000/Filtro a nivel intermunicipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2000/Filtro a nivel intermunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

#### Filtro de estados 
filtro_out <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2000/Filtro a nivel intermunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_estado)

################################################################################
## Se generan los filtros correspondientes a la matriz cuadrada por estados 
tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              filter(value > 0)

tabla1 <- migration_flows_municipality(tabla = tabla, 
                                       filtro_mun = filtro,
                                       filtro_est = filtro_est,
                                       filtro_mig = filtro_mig, 
                                       filtro_out = filtro_out, 
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group_mun = "Otros municipios",
                                       group_est = "Otros estados")

## Se sacan los flujos migratorios que pertencen a otros estados
#tabla_estados <- sapply(1:32, function(i){
#                                 tabla1[[i]] %>%
#                                  as.data.frame() %>%
#                                   adorn_totals(c("row", "col"), 
#                                                 fill = "-", 
#                                                  na.rm = TRUE, 
#                                                   ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                    select(`Otros estados`) %>%
#                                     slice(nrow(.)) %>%
#                                      mutate(`Otros estados` = .$`Otros estados`/10) %>%
#                                       pull(`Otros estados`)
#})

## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:32, function(i){
#                               tabla1[[i]] %>%
#                                as.data.frame() %>%
#                                 select(-c(`Otros estados`)) %>%
#                                  adorn_totals(c("row", "col"), 
#                                                fill = "-", 
#                                                 na.rm = TRUE, 
#                                                  ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                    slice(nrow(.)) %>%
#                                     mutate(Total = .$Total/50) %>%
#                                      pull(Total)
#})

## Se guardan las matrices de migración reciente para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz MR5a a nivel intermunicipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}

saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Tabla MR5a a nivel intermunicipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Tabla MR5a a nivel intermunicipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Inmigrantes",  
                        Emigrantes = "Emigrantes")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Inmigrantes",  
                                  Emigrantes = "%Emigrantes")

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, totales_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz MR5a a nivel intermunicipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}

Dada la magnitud de municipios en algunos estados se seleccionan solo algunos de ellos.

tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Tabla MR5a a nivel intermunicipal.RDS"))

## Paleta de colores
#paleta <- colorRampPalette(pals::kovesi.linear_bmy_10_95_c78(100))(50)
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Municipio/01_Migracion reciente/2000/ChordDiagram de MR5a desagregado a nivel intermunicipal.pdf"

## Gráficos a nivel intermunicipal
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Municipio/01_Migracion reciente/2000/Etiquetas a nivel intermunicipal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Gráfico Sankey

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel intermunicipal 2000.RData"))

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character)

################################################################################
################################## Filtro ######################################
Inmigrantes  <- Migrantes %>%
                 as.data.frame() %>%
                  tibble::rownames_to_column(var = "rn") %>% 
                   melt(., id.vars = "rn", variable.name = "cn") %>%
                    mutate_if(is.factor, as.character) %>%
                     mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                      filter(value > 0) %>%
                       group_by(rn) %>% 
                        summarise(Inmigrantes = sum(value, na.rm = TRUE)) 

Emigrantes <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                    filter(value > 0) %>%
                     group_by(cn) %>% 
                      summarise(Emigrantes = sum(value, na.rm = TRUE))     

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value < 5000) %>% 
               pull(rn)
################################################################################

tabla1 <-  lapply(1:32, function(x){
                   tabla %>%
                    mutate(rn = case_when(substr(.$rn, 1, 3) %in% estados[x] & .$rn %in% filtro ~ paste0(estados[x], " Otros municipios (", estados[x], ")"),
                                          substr(.$rn, 1, 3) %in% estados[x]  & .$rn %nin% filtro ~ .$rn,
                                          substr(.$rn, 1, 3) %nin% estados[x] ~ paste0(nom_estados[as.numeric(substr(.$rn, 1, 3))])),
                           cn = case_when(substr(.$cn, 1, 3) %in% estados[x] & .$cn %in% filtro ~ paste0(estados[x], " Otros municipios (", estados[x], ")"),
                                          substr(.$cn, 1, 3) %in% estados[x] & .$cn %nin% filtro ~ .$cn,
                                          substr(.$cn, 1, 3) %nin% estados[x] ~ paste0(nom_estados[as.numeric(substr(.$cn, 1, 3))]))) %>%
                     mutate(value = ifelse(.$rn != .$cn & (substr(.$rn, 1, 3) %in% estados[x] | substr(.$cn, 1, 3) %in% estados[x]), .$value, 0)) %>%
                      filter(value > 0)  
  }
)
p <- lapply(1:32, function(x){
             tabla1[[x]] %>% 
              ggplot(aes(axis1 = rn, 
                          axis2 = cn, 
                           y = value),  # c("value", "freq", "tasa")
                      reverse = FALSE, 
                       na.rm = TRUE) +
               geom_alluvium(aes(fill = rn),
                              curve_type = "quintic", 
                               color = "transparent", 
                                alpha = 0.85, 
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                 geom_stratum(aes(fill = cn), 
                               color = "white", 
                                alpha = 0.65,  
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                  geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                                       fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                   stat = "stratum", 
                                    size = 3, 
                                     direction = "y", 
                                      nudge_x = -.2,
                                       min.segment.length = unit(1, "lines"),
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                   geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                        fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                    stat = "stratum", 
                                     size = 3,
                                      direction = "y", 
                                       nudge_x = .2, 
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                     theme_void() + 
                      theme(plot.margin = margin(t = 1, r = 5, b = 1, l = 0, "cm"),
                             text = element_text(family = "montserrat"),
                              axis.text = element_blank(),
                               axis.title = element_blank(),
                                strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                 legend.key.size = unit(0.5, "cm"),
                                  legend.text = element_text(size = 7, family = "montserrat"),
                                   legend.position = c(1.02, .5)) + 
                       scale_x_discrete(expand = c(-0.1, 0.5)) +
                        scale_fill_viridis_d(option = "A", end = 1, begin = 0.2) +
                         guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                          labs(fill = "", 
                               color = "")
  }
)

path = paste0(here::here(), "/Graficos/Municipio/01_Migracion reciente/2000/GSankey de MR5a desagregado a nivel intermunicipal.pdf")
ggexport(list = p, width = 18, height = 10, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de migración

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <=130) %>%
                 group_by(CVE_MUN) %>%
                  summarise(Pob.5ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Matriz de migracion reciente a nivel intermunicipal 2000.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>%
                gather(CVE_MUN, Value, -rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_RES) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_RES) %>%
                     group_by(CVE_MUN_RES) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_RES") 

tabla <- Pob.Total %>%
          left_join(., Pob.5ymas, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Indicadores de MR5a a nivel intermunicipal 2000.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2000/Indicadores de MR5a a nivel intermunicipal 2000.RData"))
Indicadores de migración reciente
Nivel intermunicipal
CVE_MUN Pob.Total Pob.5ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 641 132 565 045 0 29 923 15 357 14 566 45 280 9.9 5.1 4.8 −30 714
001002 37 617 32 042 0 1 024 217 807 1 241 5.9 1.2 4.6 −434
001003 51 200 43 800 0 1 092 855 237 1 947 4.6 3.6 1.0 −1 710
001004 12 590 10 834 0 235 98 137 333 4.0 1.7 2.3 −196
001005 63 884 54 820 0 1 578 353 1 225 1 931 5.3 1.2 4.1 −706
001006 34 123 29 923 0 708 362 346 1 070 4.4 2.3 2.2 −724
001007 41 501 35 055 0 550 387 163 937 2.9 2.0 0.9 −774
001008 7 246 6 263 0 59 275 −216 334 1.7 8.1 −6.4 −550
001009 16 479 14 352 0 78 172 −94 250 1.0 2.2 −1.2 −344
001010 15 027 12 845 0 99 316 −217 415 1.4 4.5 −3.1 −632
001011 19 979 17 058 0 490 157 333 647 5.3 1.7 3.6 −314
002001 367 281 323 690 0 27 776 11 042 16 734 38 818 16.1 6.4 9.7 −22 084
002002 761 169 678 495 0 36 374 12 508 23 866 48 882 10.1 3.5 6.6 −25 016
002003 77 011 67 014 0 7 723 1 997 5 726 9 720 21.4 5.5 15.9 −3 994
002004 1 207 045 1 056 227 0 128 309 33 596 94 713 161 905 22.7 5.9 16.7 −67 192
002005 63 504 55 753 0 7 226 1 345 5 881 8 571 24.2 4.5 19.7 −2 690
003001 63 021 55 998 0 1 444 1 213 231 2 657 4.9 4.1 0.8 −2 426
003002 45 135 39 813 0 2 743 1 574 1 169 4 317 12.9 7.4 5.5 −3 148
003003 194 714 174 999 0 10 895 9 042 1 853 19 937 11.8 9.8 2.0 −18 084
003008 104 665 91 462 0 15 786 3 137 12 649 18 923 32.2 6.4 25.8 −6 274
Fuente: Estimaciones del CONAPO.

Referencias

Librerias que se usaron en el documento

package loadedversion source
Cairo 1.6-1 CRAN (R 4.3.1)
chorddiag 0.1.3 Github ()
circlize 0.4.15 CRAN (R 4.3.1)
doMC 1.3.5 R-Forge (R 4.3.1)
dplyr 1.1.3 CRAN (R 4.3.2)
expss 0.11.6 CRAN (R 4.3.1)
foreach 1.5.2 CRAN (R 4.3.1)
ggalluvial 0.12.5 CRAN (R 4.3.1)
ggplot2 3.4.3 CRAN (R 4.3.1)
ggpubr 0.6.0 CRAN (R 4.3.1)
ggrepel 0.9.3 CRAN (R 4.3.1)
ggsankey 0.0.99999 Github ()
gt 0.10.0 CRAN (R 4.3.1)
haven 2.5.3 CRAN (R 4.3.1)
Hmisc 5.1-0 CRAN (R 4.3.1)
iterators 1.0.14 CRAN (R 4.3.1)
janitor 2.2.0 CRAN (R 4.3.1)
kableExtra 1.3.4 CRAN (R 4.3.1)
knitr 1.45 CRAN (R 4.3.2)
maditr 0.8.3 CRAN (R 4.3.1)
mapview 2.11.0 CRAN (R 4.3.1)
Matrix 1.6-1.1 CRAN (R 4.3.1)
network 1.18.1 CRAN (R 4.3.1)
openxlsx 4.2.5.2 CRAN (R 4.3.1)
reshape2 1.4.4 CRAN (R 4.3.1)
sjlabelled 1.2.0 CRAN (R 4.3.1)
sna 2.7-1 CRAN (R 4.3.1)
srvyr 1.2.0 CRAN (R 4.3.1)
statnet.common 4.9.0 CRAN (R 4.3.1)
stringr 1.5.0 CRAN (R 4.3.1)
survey 4.2 Github ()
survival 3.5-5 CRAN (R 4.3.1)
tibble 3.2.1 CRAN (R 4.3.1)
tidyr 1.3.0 CRAN (R 4.3.1)

Creative Commons Licence
This work by Diana Villasana Ocampo is licensed under a Creative Commons Attribution 4.0 International License.